package com.it.flowable.dto;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.flowable.bpmn.model.FormProperty;
import org.flowable.bpmn.model.FormValue;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@NoArgsConstructor
@Data
@Accessors(chain = true)
public class FormInfoDto {

    /** 表单属性ID */
    private String id;
    
    /** 表单属性名称 */
    private String name;
    
    /** 表单控件类型 */
    private String type;
    
    /** 是否必填 */
    private boolean required;
    
    /** 是否可读 */
    private boolean readable = true;
    
    /** 是否可写 */
    private boolean writeable = true;
    
    /** 值表达式 */
    private String expression;
    
    /** 变量名称 */
    private String variable;
    
    /** 默认值表达式 */
    private String defaultExpression;
    
    /** 日期格式（针对日期类型） */
    private String datePattern;
    
    /** 表单值选项列表 */
    private List<FormValueDto> formValues = new ArrayList<>();
    
    /**
     * 从 FormProperty 构造 FormInfoDto
     */
    public FormInfoDto(FormProperty formProperty) {
        if (formProperty != null) {
            // 基础属性映射
            this.id = formProperty.getId();
            this.name = formProperty.getName();
            this.type = formProperty.getType();
            this.expression = formProperty.getExpression();
            this.variable = formProperty.getVariable();
            this.defaultExpression = formProperty.getDefaultExpression();
            this.datePattern = formProperty.getDatePattern();
            this.readable = formProperty.isReadable();
            this.writeable = formProperty.isWriteable();
            this.required = formProperty.isRequired();
            
            // 转换 FormValue 到 FormValueDto
            if (formProperty.getFormValues() != null) {
                this.formValues = formProperty.getFormValues().stream()
                    .map(FormValueDto::new)
                    .collect(Collectors.toList());
            }
        }
    }
    /**
     * 转换为 FormProperty
     */
    public FormProperty toFormProperty() {
        FormProperty formProperty = new FormProperty();
        formProperty.setId(this.id);
        formProperty.setName(this.name);
        formProperty.setType(this.type);
        formProperty.setExpression(this.expression);
        formProperty.setVariable(this.variable);
        formProperty.setDefaultExpression(this.defaultExpression);
        formProperty.setDatePattern(this.datePattern);
        formProperty.setReadable(this.readable);
        formProperty.setWriteable(this.writeable);
        formProperty.setRequired(this.required);
        
        // 转换 FormValueDto 回 FormValue
        if (this.formValues != null) {
            List<FormValue> flowableFormValues = this.formValues.stream()
                .map(FormValueDto::toFormValue)
                .collect(Collectors.toList());
            formProperty.setFormValues(flowableFormValues);
        }
        
        return formProperty;
    }
}